package com.yunji.ylexpiration.Controller;

import com.yunji.ylexpiration.entity.FurnClassificationtype;
import com.yunji.ylexpiration.entity.FurnCommodity;
import com.yunji.ylexpiration.entity.FurnVarietys;
import com.yunji.ylexpiration.service.FurnClassificationtypeService;
import com.yunji.ylexpiration.service.FurnCommodityService;
import com.yunji.ylexpiration.service.FurnVarietysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Random;

@Controller
public class FurnCommodityController {
    @Autowired
    private FurnCommodityService furnCommodityService;

    @Autowired
    private FurnClassificationtypeService furnClassificationtypeService;

    @Autowired
    private FurnVarietysService furnVarietysService;

    //首页加载数据
    @GetMapping("index.html")
    public String index(HttpSession session){
        //摆件花艺
        Object furnCommodities01 = session.getAttribute("furnCommodities");
        Object furnCommodities02 = session.getAttribute("furnCommodities2");
        if(furnCommodities01==null||furnCommodities02==null) {
            //随机6位
            List<FurnCommodity> furnCommodities1 = furnCommodityService.selectById();
            //取出所有id并存入数组
            Integer [] result = new Integer[furnCommodities1.size()];
            int i1 = 0;
            for (FurnCommodity furnCommodity:furnCommodities1) {
                Integer id = furnCommodity.getId();
                result[i1] = id;
                i1++;
            }
            //创建随机数
            Random r =new Random();

            //随机个数,随机的id并存入数组
            int[]b =new int[6];
            int temp = 0;
            int index=0;
            for(int i=0;i<6;i++){
                index =r.nextInt(result.length-i);//取a数组的下标数
                b[i]=result[index];//把取到的数存于新建的数组b中
                temp =result[index]; //6次循环取到的随机数按序交换到数组最后面
                result[index]=result[result.length-1-i];
                result[result.length-1-i]=temp;
            }
            //随机分层
            Integer [] furnId1 = new Integer[3];
            Integer [] furnId2 = new Integer[3];
            int id1 = 0;
            int id2 = 0;
            for(int i=0;i<6;i++){
                if(i<3){
                    furnId1[id1] = b[i];
                    id1++;
                }else{
                    furnId2[id2] = b[i];
                    id2++;
                }
            }


            List<FurnCommodity> furnCommodities = furnCommodityService.selectByClassificationtype_idandLimit6(furnId1);
            List<FurnCommodity> furnCommodities2 = furnCommodityService.selectByClassificationtype_idandLimit6(furnId2);
            if (furnCommodities == null||furnCommodities2 == null) {
                return "";
            } else {
                session.setAttribute("furnCommodities", furnCommodities);
                session.setAttribute("furnCommodities2", furnCommodities2);
            }
        }
        //墙壁挂画
        Object furnCommodities03 = session.getAttribute("furnCommodities3");
        Object furnCommodities04 = session.getAttribute("furnCommodities4");
        Object furnCommodities05 = session.getAttribute("furnCommodities5");
        if(furnCommodities03==null||furnCommodities04==null||furnCommodities05==null) {
            //随机16位
            List<FurnCommodity> furnCommodities = furnCommodityService.selectById2();
            //取出所有id并存入数组
            Integer [] result = new Integer[furnCommodities.size()];
            int i1 = 0;
            for (FurnCommodity furnCommodity:furnCommodities) {
                Integer id = furnCommodity.getId();
                result[i1] = id;
                i1++;
            }
            //创建随机数
            Random r =new Random();

            //随机个数,随机的id并存入数组
            int[]b =new int[12];
            int temp = 0;
            int index=0;
            for(int i=0;i<12;i++){
                index =r.nextInt(result.length-i);//取a数组的下标数
                b[i]=result[index];//把取到的数存于新建的数组b中
                temp =result[index]; //6次循环取到的随机数按序交换到数组最后面
                result[index]=result[result.length-1-i];
                result[result.length-1-i]=temp;
            }
            //随机分层
            Integer [] furnId3 = new Integer[4];
            Integer [] furnId4 = new Integer[4];
            Integer [] furnId5 = new Integer[4];
            int id3 = 0;
            int id4 = 0;
            int id5 = 0;
            for(int i=0;i<12;i++){
                if(i<4){
                    furnId3[id3] = b[i];
                    id3++;
                }else if(i>3&&i<8){
                    furnId4[id4] = b[i];
                    id4++;
                } else{
                    furnId5[id5] = b[i];
                    id5++;
                }
            }
            List<FurnCommodity> furnCommodities3 = furnCommodityService.selectByClassificationtype_parent_idandLimit16(furnId3);
            List<FurnCommodity> furnCommodities4 = furnCommodityService.selectByClassificationtype_parent_idandLimit16(furnId4);
            List<FurnCommodity> furnCommodities5 = furnCommodityService.selectByClassificationtype_parent_idandLimit16(furnId5);
            if (furnCommodities3 == null||furnCommodities4==null||furnCommodities5==null) {
                return "";
            } else {
                session.setAttribute("furnCommodities3", furnCommodities3);
                session.setAttribute("furnCommodities4", furnCommodities4);
                session.setAttribute("furnCommodities5", furnCommodities5);
            }
        }
        return "index";
    }
    @RequestMapping("proDetail.html")
    public String proDetail(){

        return "proDetail";
    }
    @RequestMapping("cart.html")
    public String cart(){
        return "cart";
    }

    //跳转到装饰摆件、布艺软饰
    @GetMapping("product.html")
    public String pro(String typename,String typename2,String url,HttpSession session){
        session.setAttribute("typename",typename);
        session.setAttribute("typename2",typename2);
        session.setAttribute("url",url);
        return "redirect:"+url;
    }

    //分页查询，滚轮查询,条件查询
    @GetMapping("product2.html")
    @ResponseBody
    public List<FurnCommodity> product(Integer snum,Integer price, String typename,String typename2,@RequestParam(defaultValue = "1")Integer pagenum,String url,HttpSession session){
        //一页为多少个
        Integer pagecount = 20;

        //查询总数量
        List<FurnCommodity> furnCommodities1 = furnCommodityService.CountByClassificationtype_parent_id(typename,typename2);
        Integer count = (int) Math.ceil(furnCommodities1.size()/20);
        if(furnCommodities1.size()<20){
            count=1;
        }
        if(pagenum>count){
            return null;
        }
        //页码
        Integer pnum = (pagenum*pagecount)-pagecount;
        //根据条件,分页查询
        List<FurnCommodity> furnCommodities = furnCommodityService.selectByClassificationtype_parent_id(snum,price,typename,typename2,pnum,pagecount);
        if(furnCommodities!=null&&furnCommodities.size()>0){
            return furnCommodities;
        }
        return furnCommodities;
    }

    //墙式壁挂分类查询
    @GetMapping("paint.html")
    public String paint(HttpSession session){
        String typename = "墙式壁挂";
        Object furnCommodities011 = session.getAttribute("furnCommodities01");
        Object furnCommodities021 = session.getAttribute("furnCommodities02");
        Object furnCommodities031 = session.getAttribute("furnCommodities03");
        Object furnCommodities041 = session.getAttribute("furnCommodities04");
        if(furnCommodities011==null||furnCommodities021==null||furnCommodities031==null||furnCommodities041==null) {
            //随机19位
            List<FurnCommodity> furnCommodities4 = furnCommodityService.selectByClassId(typename, null);
            //取出所有id并存入数组
            Integer[] result = new Integer[furnCommodities4.size()];
            int i1 = 0;
            for (FurnCommodity furnCommodity : furnCommodities4) {
                Integer id = furnCommodity.getId();
                result[i1] = id;
                i1++;
            }
            //创建随机数
            Random r = new Random();

            //随机个数,随机的id并存入数组
            int[] b = new int[19];
            int temp = 0;
            int index = 0;
            for (int i = 0; i < 19; i++) {
                index = r.nextInt(result.length - i);//取a数组的下标数
                b[i] = result[index];//把取到的数存于新建的数组b中
                temp = result[index]; //6次循环取到的随机数按序交换到数组最后面
                result[index] = result[result.length - 1 - i];
                result[result.length - 1 - i] = temp;
            }
            //随机分层
            Integer[] furnId1 = new Integer[6];
            Integer[] furnId2 = new Integer[6];
            Integer[] furnId3 = new Integer[4];
            Integer[] furnId4 = new Integer[3];
            int id1 = 0;
            int id2 = 0;
            int id3 = 0;
            int id4 = 0;
            int f1 = furnId1.length;
            int f2 = f1+furnId2.length;
            int f3 = f2+furnId3.length;
            int f4 = f3+furnId4.length;
            for (int i = 0; i < 19; i++) {
                if (i < f1) {
                    furnId1[id1] = b[i];
                    id1++;
                } else if (i > (f1-1) && i < f2) {
                    furnId2[id2] = b[i];
                    id2++;
                } else if (i > (f2-1) && i < f3) {
                    furnId3[id3] = b[i];
                    id3++;
                }else if (i > (f3-1) && i < f4) {
                    furnId4[id4] = b[i];
                    id4++;
                }
            }

            List<FurnCommodity> furnCommodities01 = furnCommodityService.selectByClassId(typename, furnId1);
            List<FurnCommodity> furnCommodities02 = furnCommodityService.selectByClassId(typename, furnId2);
            List<FurnCommodity> furnCommodities03 = furnCommodityService.selectByClassId(typename, furnId3);
            List<FurnCommodity> furnCommodities04 = furnCommodityService.selectByClassId(typename, furnId4);
            if (furnCommodities01 == null || furnCommodities02 == null || furnCommodities03 == null || furnCommodities04 == null) {
                return "";
            } else {
                session.setAttribute("furnCommodities01", furnCommodities01);
                session.setAttribute("furnCommodities02", furnCommodities02);
                session.setAttribute("furnCommodities03", furnCommodities03);
                session.setAttribute("furnCommodities04", furnCommodities04);
            }
        }
        return "paint";
    }

    //蜡艺香薰分类查询
    @GetMapping("perfume.html")
    public String perfume(HttpSession session){
        String typename = "蜡艺香薰";
        Object furnCommodities011 = session.getAttribute("furnCommodities05");
        Object furnCommodities021 = session.getAttribute("furnCommodities06");
        if(furnCommodities011==null||furnCommodities021==null) {
            //随机19位
            List<FurnCommodity> furnCommodities4 = furnCommodityService.selectByClassId(typename, null);
            System.out.println(furnCommodities4.size());
            //取出所有id并存入数组
            Integer[] result = new Integer[furnCommodities4.size()];
            int i1 = 0;
            for (FurnCommodity furnCommodity : furnCommodities4) {
                Integer id = furnCommodity.getId();
                result[i1] = id;
                i1++;
            }
            //创建随机数
            Random r = new Random();

            //随机个数,随机的id并存入数组
            int[] b = new int[12];
            int temp = 0;
            int index = 0;
            for (int i = 0; i < 12; i++) {
                index = r.nextInt(result.length - i);//取a数组的下标数
                b[i] = result[index];//把取到的数存于新建的数组b中
                temp = result[index]; //6次循环取到的随机数按序交换到数组最后面
                result[index] = result[result.length - 1 - i];
                result[result.length - 1 - i] = temp;
            }
            //随机分层
            Integer[] furnId1 = new Integer[6];
            Integer[] furnId2 = new Integer[6];
            int id1 = 0;
            int id2 = 0;
            int f1 = furnId1.length;
            int f2 = f1+furnId2.length;
            for (int i = 0; i < 12; i++) {
                if (i < f1) {
                    furnId1[id1] = b[i];
                    id1++;
                } else if (i > (f1-1) && i < f2){
                    furnId2[id2] = b[i];
                    id2++;
                }
            }

            List<FurnCommodity> furnCommodities01 = furnCommodityService.selectByClassId(typename, furnId1);
            List<FurnCommodity> furnCommodities02 = furnCommodityService.selectByClassId(typename, furnId2);
            if (furnCommodities01 == null || furnCommodities02 == null ) {
                return "";
            } else {
                session.setAttribute("furnCommodities05", furnCommodities01);
                session.setAttribute("furnCommodities06", furnCommodities02);
            }
        }
        return "perfume";
    }

    //创意家居分类查询
    @GetMapping("idea.html")
    public String idea(HttpSession session){
        String typename = "创意家居";
        Object furnCommodities011 = session.getAttribute("furnCommodities07");
        Object furnCommodities021 = session.getAttribute("furnCommodities08");
        Object furnCommodities031 = session.getAttribute("furnCommodities09");
        Object furnCommodities041 = session.getAttribute("furnCommodities10");
        Object furnCommodities051 = session.getAttribute("furnCommodities11");
        Object furnCommodities061 = session.getAttribute("furnCommodities12");
        Object furnCommodities071 = session.getAttribute("furnCommodities13");
        if(furnCommodities011==null||furnCommodities021==null||furnCommodities031==null||furnCommodities041==null||furnCommodities051==null||furnCommodities061==null||furnCommodities071==null) {
            //随机19位
            List<FurnCommodity> furnCommodities4 = furnCommodityService.selectByClassId(typename, null);
            //取出所有id并存入数组
            Integer[] result = new Integer[furnCommodities4.size()];
            int i1 = 0;
            for (FurnCommodity furnCommodity : furnCommodities4) {
                Integer id = furnCommodity.getId();
                result[i1] = id;
                i1++;
            }
            //创建随机数
            Random r = new Random();

            //随机个数,随机的id并存入数组
            int[] b = new int[25];
            int temp = 0;
            int index = 0;
            for (int i = 0; i < 25; i++) {
                index = r.nextInt(result.length - i);//取a数组的下标数
                b[i] = result[index];//把取到的数存于新建的数组b中
                temp = result[index]; //6次循环取到的随机数按序交换到数组最后面
                result[index] = result[result.length - 1 - i];
                result[result.length - 1 - i] = temp;
            }
            //随机分层
            Integer[] furnId1 = new Integer[3];
            Integer[] furnId2 = new Integer[3];
            Integer[] furnId3 = new Integer[3];
            Integer[] furnId4 = new Integer[4];
            Integer[] furnId5 = new Integer[4];
            Integer[] furnId6 = new Integer[4];
            Integer[] furnId7 = new Integer[4];
            int id1 = 0;
            int id2 = 0;
            int id3 = 0;
            int id4 = 0;
            int id5 = 0;
            int id6 = 0;
            int id7 = 0;
            int f1 = furnId1.length;
            int f2 = f1+furnId2.length;
            int f3 = f2+furnId3.length;
            int f4 = f3+furnId4.length;
            int f5 = f4+furnId5.length;
            int f6 = f5+furnId6.length;
            int f7 = f6+furnId7.length;
            for (int i = 0; i < 25; i++) {
                if (i < f1) {
                    furnId1[id1] = b[i];
                    id1++;
                } else if (i > (f1-1) && i < f2) {
                    furnId2[id2] = b[i];
                    id2++;
                } else if (i > (f2-1) && i < f3) {
                    furnId3[id3] = b[i];
                    id3++;
                }else if (i > (f3-1) && i < f4) {
                    furnId4[id4] = b[i];
                    id4++;
                }else if (i > (f4-1) && i < f5) {
                    furnId5[id5] = b[i];
                    id5++;
                }else if (i > (f5-1) && i < f6) {
                    furnId6[id6] = b[i];
                    id6++;
                }else if (i > (f6-1) && i < f7) {
                    furnId7[id7] = b[i];
                    id7++;
                }
            }

            List<FurnCommodity> furnCommodities01 = furnCommodityService.selectByClassId(typename, furnId1);
            List<FurnCommodity> furnCommodities02 = furnCommodityService.selectByClassId(typename, furnId2);
            List<FurnCommodity> furnCommodities03 = furnCommodityService.selectByClassId(typename, furnId3);
            List<FurnCommodity> furnCommodities04 = furnCommodityService.selectByClassId(typename, furnId4);
            List<FurnCommodity> furnCommodities05 = furnCommodityService.selectByClassId(typename, furnId5);
            List<FurnCommodity> furnCommodities06 = furnCommodityService.selectByClassId(typename, furnId6);
            List<FurnCommodity> furnCommodities07 = furnCommodityService.selectByClassId(typename, furnId7);
            if (furnCommodities01 == null || furnCommodities02 == null || furnCommodities03 == null || furnCommodities04 == null||furnCommodities05==null||furnCommodities06==null||furnCommodities07==null) {
                return "";
            } else {
                session.setAttribute("furnCommodities7", furnCommodities01);
                session.setAttribute("furnCommodities8", furnCommodities02);
                session.setAttribute("furnCommodities9", furnCommodities03);
                session.setAttribute("furnCommodities10", furnCommodities04);
                session.setAttribute("furnCommodities11", furnCommodities05);
                session.setAttribute("furnCommodities12", furnCommodities06);
                session.setAttribute("furnCommodities13", furnCommodities07);
            }
        }
        return "idea";
    }

    //搜索框
    @GetMapping("search.html")
    public String search(String search,HttpSession session){

        session.setAttribute("search",search);
        return "search";
    }

    //搜索框模糊查询
    @GetMapping("dosearch.html")
    @ResponseBody
    public List<FurnCommodity> dosearch(Integer snum,Integer price, String search,Integer pagenum,HttpSession session){
        //一页为多少个
        Integer pagecount = 20;
        //判断是否为空,算出一页数为多少个数
        if(pagenum==null){
            pagenum=1;

        }
        //查询总数量
        Integer count = null;
        Integer maxcount = null;
        List<FurnCommodity> furnCommodities1 = furnCommodityService.SearchByCount(null,search,null);
        if(furnCommodities1==null||furnCommodities1.size()==0) {
            List<FurnCommodity> furnCommodities2 = furnCommodityService.SearchByCount(null,null,search);
            if(furnCommodities2==null||furnCommodities2.size()==0){
                List<FurnCommodity> furnCommodities3 = furnCommodityService.SearchByCount(search,null,null);
                if(furnCommodities3==null||furnCommodities3.size()==0) {
                    return null;
                }else{
                    maxcount = furnCommodities3.size();
                    count = (int) Math.ceil(furnCommodities3.size() / 20);
                }
            }else{
                maxcount = furnCommodities2.size();
                count = (int) Math.ceil(furnCommodities2.size() / 20);
            }
        }else {
            maxcount = furnCommodities1.size();
            count = (int) Math.ceil(furnCommodities1.size() / 20);
        }
        if(maxcount<20){
            count=1;
        }
        if(pagenum>count){
            return null;
        }
        //页码
        Integer pnum = (pagenum*pagecount)-pagecount;
        //根据条件,分页查询
        List<FurnCommodity> furnCommodities = furnCommodityService.searchByProductName(snum, price, null,search,null, pagenum, pagecount);
        if(furnCommodities==null||furnCommodities.size()==0){
            List<FurnCommodity> furnCommodities2 = furnCommodityService.searchByProductName(snum, price, null, null, search, pagenum, pagecount);
            if(furnCommodities2==null||furnCommodities2.size()==0){
                List<FurnCommodity> furnCommodities3 = furnCommodityService.searchByProductName(snum, price, search, null, null, pagenum, pagecount);
                if(furnCommodities3.size()==0||furnCommodities3==null) {
                    return null;
                }else {
                    return furnCommodities3;
                }
            }else{
                return furnCommodities2;
            }
        }else {
            return furnCommodities;
        }
    }

    //详细信息
    @GetMapping("proDetail.html")
    public String selectDetailById(Integer id,Model model){
        //查询商品信息
        FurnCommodity furnCommodity = furnCommodityService.selectDetailById(id);
        //查询类型信息
        FurnClassificationtype furnClassificationtype = furnClassificationtypeService.selectByparentIdByid(furnCommodity.getClassificationtypeId());
        FurnClassificationtype furnClassificationtype1 = furnClassificationtypeService.selectByparentIdByid(furnCommodity.getClassificationtypeParentId());

        //查询颜色分类信息
        List<FurnVarietys> furnVarietys = furnVarietysService.selectByscoreId(furnCommodity.getCorreId());

        //为你推荐8条相关数据
        List<FurnCommodity> furnCommodities5 = furnCommodityService.recommendByfirve(furnCommodity.getClassificationtypeId(), furnCommodity.getClassificationtypeParentId());

        //取出所有id并存入数组
        Integer [] result = new Integer[furnCommodities5.size()];
        int i1 = 0;
        for (FurnCommodity furnCommodity8:furnCommodities5) {
            Integer id2 = furnCommodity8.getId();
            result[i1] = id2;
            i1++;
        }
        //创建随机数
        Random r =new Random();

        //随机个数,随机的id并存入数组
        int[]b =new int[7];
        int temp = 0;
        int index=0;
        for(int i=0;i<7;i++){
            index =r.nextInt(result.length-i);//取a数组的下标数
            b[i]=result[index];//把取到的数存于新建的数组b中
            temp =result[index]; //6次循环取到的随机数按序交换到数组最后面
            result[index]=result[result.length-1-i];
            result[result.length-1-i]=temp;
        }
        //随机分层
        Integer [] furnId = new Integer[7];
        int id8 = 0;
        for(int i=0;i<7;i++){
            furnId[id8] = b[i];
            id8++;
        }
        List<FurnCommodity> furnCommodities8 = furnCommodityService.selectByClassId(null, furnId);

        //详细信息中的猜你喜欢获取销量前15条数据
        List<FurnCommodity> furnCommodities05 = furnCommodityService.RelatedByLimit15(furnCommodity.getClassificationtypeId(), furnCommodity.getClassificationtypeParentId(), 0, 5);
        List<FurnCommodity> furnCommodities510 = furnCommodityService.RelatedByLimit15(furnCommodity.getClassificationtypeId(),furnCommodity.getClassificationtypeParentId(),5,5);
        List<FurnCommodity> furnCommodities105 = furnCommodityService.RelatedByLimit15(furnCommodity.getClassificationtypeId(),furnCommodity.getClassificationtypeParentId(),10,5);

        model.addAttribute("furnCommodities05",furnCommodities05);
        model.addAttribute("furnCommodities510",furnCommodities510);
        model.addAttribute("furnCommodities105",furnCommodities105);
        model.addAttribute("furnCommodity",furnCommodity);
        model.addAttribute("furnClassificationtype",furnClassificationtype);
        model.addAttribute("furnClassificationtype1",furnClassificationtype1);
        model.addAttribute("furnVarietys",furnVarietys);
        model.addAttribute("furnCommodities8",furnCommodities8);
        return "proDetail";
    }

    //快速浏览
    @GetMapping("quick.html")
    @ResponseBody
    public FurnCommodity selectDetailById2(@RequestParam(defaultValue = "1")Integer id, Model model){
        //查询商品信息
        FurnCommodity furnCommodity = furnCommodityService.selectDetailById(id);
        if(furnCommodity==null){
            return null;
        }
        return furnCommodity;
    }
    @GetMapping("quick2.html")
    @ResponseBody
    public List<FurnVarietys> selectByscoreId(@RequestParam(defaultValue = "1")Integer id, Model model){
        //查询颜色分类信息
        List<FurnVarietys> furnVarietys = furnVarietysService.selectByscoreId(id);
        if(furnVarietys==null||furnVarietys.size()==0){
            return null;
        }
        return furnVarietys;
    }
    @GetMapping("quick3.html")
    @ResponseBody
    public Double selectByimg(String img, Model model){
        //根据对应颜色分类查询对应价格
        Double aDouble = furnVarietysService.selectByimg(img);
        if(aDouble==null){
            return null;
        }
        return aDouble;
    }
}
